package com.chuangjiangx.member.stored.web.controller;

import com.chuangjiangx.commons.CurrentThreadContext;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.basic.web.controller.BaseController;
import com.chuangjiangx.member.basic.web.interceptor.Login;
import com.chuangjiangx.member.stored.web.request.SearchStoredInfoRequest;
import com.chuangjiangx.member.stored.web.response.StoredDetailInfoResponse;
import com.chuangjiangx.member.stored.web.response.StoredDetailListResponse;
import com.chuangjiangx.member.common.utils.BeanUtils;
import com.chuangjiangx.member.stored.ddd.query.MbrStoredStreamQuery;
import com.chuangjiangx.member.stored.ddd.dal.condition.QueryStoredStreamListCondition;
import com.chuangjiangx.member.stored.ddd.dal.dto.StoredStreamListForH5;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredDetailListDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamInfoForH5DTO;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrStoredType;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 储值明细
 */
@Slf4j
@RestController
@RequestMapping(value = "/h5/stored", produces = "application/json")
public class StoredInfoController extends BaseController {
    @Autowired
    private MbrStoredStreamQuery mbrStoredStreamQuery;
    /**
     * 获取储值明细列表
     *
     * @param type 查询参数, 0-全部 1-收入 2-支出
     * @return com.chuangjiangx.commons.response.UnderlineResponse
     * @map storedDetailList List
     * @map page
     * @see StoredDetailListResponse
     */
    @RequestMapping(value = "/search-stored-detail-list")
    @ResponseBody
    @Login
    public Response searchStoredDetailList(Integer type) {

        MbrUserContext memeberValue = (MbrUserContext) CurrentThreadContext.getCurrentUser();

        //查询参数对象转换
        QueryStoredStreamListCondition condition = new QueryStoredStreamListCondition();
        /**
         * 0 - null
         * 1 - (1,3,4)
         * 2 - (2)
         */
        if(1 == type){
            //查询收入
            condition.setTypes(Arrays.asList(
                    MbrStoredType.RECHARGE.value,
                    MbrStoredType.REFUND.value,
                    MbrStoredType.RECHARGE_GIFT.value
            ));
        }else if(2 == type){
            //查询支出
            condition.setTypes(Arrays.asList(
                    MbrStoredType.CONSUMER.value
            ));
        }

        condition.setMerchantId(memeberValue.getMerchantId());
        condition.setMemberId(memeberValue.getMemberId());

        List<StoredStreamListForH5> list = mbrStoredStreamQuery.searchStoredStreamListForH5(condition);
        Map<Date, List<StoredStreamListForH5>> dateListMap = list
                .stream()
                .collect(Collectors.groupingBy(o -> DateUtils.truncate(o.getDateTime(), Calendar.DAY_OF_MONTH)));

        List<StoredDetailListResponse> respList = new ArrayList<>(64);
        dateListMap.keySet()
                .stream()
                .sorted(Comparator.reverseOrder())
                .forEach(date -> {
                    StoredDetailListResponse res = new StoredDetailListResponse();
                    res.setDateTime(date);
                    res.setStoredDetailListDTOs(com.chuangjiangx.commons.BeanUtils.convertCollection(dateListMap.get(date),StoredDetailListDTO.class));
                    respList.add(res);
                });
        return ResponseUtils.success("storedDetailList", respList);
    }

    /**
     * 根据id查询储值明细详情
     *
     * @param request 储值明细ID
     * @return com.chuangjiangx.commons.response.UnderlineResponse
     * @see StoredDetailInfoResponse
     */
    @RequestMapping(value = "/search-stored-detail")
    @ResponseBody
    @Login
    public Response searchStoredDetailInfo(@Validated @RequestBody SearchStoredInfoRequest request) {
        StoredDetailInfoResponse response = new StoredDetailInfoResponse();
        StoredStreamInfoForH5DTO h5DTO = mbrStoredStreamQuery.searchStoredStreamInfoForH5(request.getId());
        BeanUtils.convertBean(h5DTO, response);
        return ResponseUtils.success(response);
    }

}
